﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;

namespace Web
{
    public partial class PostBook : System.Web.UI.Page
    {
        private int SubjectID = 0;
        private int editid = 0;
        private int delid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterBook"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }

            if (Request.QueryString["SubjectID"] == null)
            {
                Utility.ShowJsAlertAndCloseRefresh("一般系统保护性错误", this);
            }
            else
            {
                if (!Utility.chk_int(Request.QueryString["SubjectID"]))
                {
                    Utility.ShowJsAlertAndCloseRefresh("一般系统保护性错误", this);
                }
                else
                {
                    SubjectID = Convert.ToInt32(Request.QueryString["SubjectID"].Trim());
                }
            }
            if (Request.QueryString["editid"] != null)
            {
                editid = Convert.ToInt32(Request.QueryString["editid"].Trim());
            }
            if (Request.QueryString["delid"] != null)
            {
                delid = Convert.ToInt32(Request.QueryString["delid"].Trim());
            }
            if (!IsPostBack)
            {
                if (editid > 0)
                {
                    BookInfo bookInfo = new BookInfo();
                    bookInfo = Book.GetBookInfoById(editid);
                    BookName.Text = bookInfo.Name;
                    BookKeyword.Text = bookInfo.Keyword;
                }
                if (delid > 0)
                {
                    Book.DelBook(delid);
                    Response.Redirect("admindata.aspx");
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (BookName.Text.Trim() == "" || BookKeyword.Text.Trim() == "")
            {
                Utility.ShowJsAlert("教材版本名称和关键字不能为空！", this);
            }
            else
            {
                BookInfo bookInfo = new BookInfo();
                bookInfo.Name = BookName.Text.Trim();
                bookInfo.Keyword = BookKeyword.Text.Trim();
                bookInfo.Subject = SubjectID;
                if (editid > 0)
                {
                    if (!Book.CheckBookExistByKeyword(BookKeyword.Text.Trim(),editid))
                    {
                        bookInfo.Id = editid;
                        Book.EditBook(bookInfo);
                        Utility.ReloadTopPage(this);
                    }
                    else
                    {
                        Utility.ShowJsAlert("关键字已存在，请更换",this);
                    }
                }
                else
                {
                    if (!Book.CheckBookExistByName(BookName.Text.Trim(), BookKeyword.Text.Trim(), SubjectID))
                    {
                        Book.AddBook(bookInfo);
                        Utility.ReloadTopPage(this);
                    }
                    else
                    {
                        Utility.ShowJsAlert("该教材版本已经存在，或关键字重复", this);
                    }
                }
            }
        }
    }
}
